﻿package com.skillscoach
{
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.net.URLRequest;
	import ru.etcs.media.WaveSound;
	import flash.media.Sound;
	import flash.events.ProgressEvent;
	import ru.etcs.events.WaveSoundEvent;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.media.SoundChannel;

	public class DataObject
	{
		private var editInfo:Array = new Array(); 
		public var frameArray:Array = new Array(); 
		private var currentInfo:EditInfoObject;
		private var sound:WaveSound;
		private var channel:SoundChannel;
		public var waitMc:MovieClip;
		
		
		
		public function DataObject()
		{
		}
		public function getFrameNumber():Array
		{
			return frameArray ;
		}
		public function setInfo(xml:XML):void
		{

			
			var info:EditInfoObject;
			
			try{
				info = getInfoByCuePoint(xml.cuePoint);
				info.cuePoint = xml.cuePoint;
				info.imageUrl = xml.savedFrame;
				info.soundUrl = xml.savedAudio;
				
			}catch(e) {
				frameArray.push(xml.cuePoint);
				info = new EditInfoObject();
				info.cuePoint = xml.cuePoint;
				info.imageUrl = xml.savedFrame;
				info.soundUrl = xml.savedAudio;
				editInfo.push(info);
			}
			frameArray.sort(Array.NUMERIC)
			
			
		}
		public function HideAllImages():void
		{
			for (var i:int = 0; i < editInfo.length; i++) 
			{
				var info:EditInfoObject = editInfo[i];
				 info.image.visible = false;
			}
		}
		public function HideImageByCuePoint(_name:Number):void
		{
			try{
				channel.stop();
			}catch(e){
				
			}
			try{
				trace(_name + "HideImageByCuePoint HideImageByCuePoint HideImageByCuePoint")
				currentInfo = getInfoByCuePoint(_name);
				Bitmap(currentInfo.image).visible = false;
			}catch(e){
				
			}
		}
		public function showImageByCuePoint(_name:Number):void
		{
			currentInfo = getInfoByCuePoint(_name);
			
			Bitmap(currentInfo.image).visible = true;

			if(currentInfo.soundUrl.indexOf(".wav") != -1)
			{
				waitMc.visible = true;
				var request:URLRequest = new URLRequest(currentInfo.soundUrl);				
				sound = new WaveSound();
				sound.load(request);
				sound.addEventListener(Event.COMPLETE, soundDownloadingCompleteHandler);
				sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				sound.addEventListener(WaveSoundEvent.DECODE_ERROR,decodeErrorHandler);
				sound.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
				sound.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler);
			}
				
		}
		private function soundDownloadingCompleteHandler(event:Event):void {
			waitMc.visible = false;
			waitMc.loadTxt.text ="";
			channel = sound.play();
		}
		
		private function progressHandler(event:ProgressEvent):void {
			trace("test")
		//field.text = text+'\nLoaded bytes: '+event.bytesLoaded.toString()+'/'+event.bytesTotal.toString();
			waitMc.loadTxt.text = Math.round((event.bytesLoaded/event.bytesTotal)*100) +"% Audio Loading...";
		}
		
		private function decodeErrorHandler(event:WaveSoundEvent):void {
		//field.text = text+'\nDecode error…';
		}
		
		private function ioErrorHandler(event:IOErrorEvent):void {
		//field.text = text+'\nI/O Error…';
		}
		
		private function httpStatusHandler(event:HTTPStatusEvent):void {
		//text += '\nHTTP Status: '+event.status;
		}
		
		public function getInfoByCuePoint(_name:Number):EditInfoObject
		{
			for (var i:int = 0; i < editInfo.length; i++) 
			{
				var info:EditInfoObject = editInfo[i];
				if(info.cuePoint == _name)
				{
					return info;
				}
			}
			
			return null;
		}
		public function deleteInfoByCuePoint(_name:Number):void
		{
			for (var i:int = 0; i < editInfo.length; i++) 
			{
				var info:EditInfoObject = editInfo[i];
				if(info.cuePoint == _name)
				{
					info.image.visible = false;
					editInfo.splice(i,1);
				}
			}
			for (i = 0; i < frameArray.length; i++) 
			{
				if(frameArray[i] == _name)
				{
					frameArray.splice(i,1);
				}
			}
			
		}
		/*public function setWavByName(_name:Number,img:*):void
		{
			array[_name+"_wav"] = img;
		}
		public function getWavByName(_name:Number):MovieClip
		{
			return array[_name+"_wav"] ;
		}*/
	}
}